/*分页组件*/
'use strict';

var React = require('react');
var Router = require('react-router'); 
var Link = Router.Link;
var Route = Router.Route;
var RouteHandler = Router.RouteHandler;

var PageComponent = React.createClass({
    getInitialState:function(){
        return {
            reactElm:[],
            prevActive:'',
            nextActive:''
        };
    },
    propTypes:{
        optionalNumber: React.PropTypes.number
    },
    checkActive:function(){
            var list=[];
            var self = this;
            var offset ;
            if (this.props.maxSize==5) {
                offset=3;
            }else if (this.props.maxSize==10) {
                offset=5;
            }
            // 初始化检查　上下页是否可点击
            this.setState({prevActive:this.props.pageNo - offset>=1?'active':''}); 
            this.setState({nextActive:this.props.pageNo+parseInt(offset) < this.props.totalItems?'active':''}); 

            list.push(<li className={this.state.prevActive}><a href="javascript:;" aria-label="Previous" onClick={self.handlerPrev} >&laquo;</a></li>);
            for (var i = 1; i <= this.props.totalItems; i++) {
                list.push(<li className={this.props.pageNo==i?'active':''} key={i} ><a href="javascript:;" onClick={self.handlerLink()}  >{i}</a></li>);
            };    
            list.push(<li className={this.state.nextActive}><a href="javascript:;" aria-label="Next" onClick={self.handlerNext}  >&raquo;</a></li>);
            this.setState({reactElm:list});
      },
      componentDidMount:function(){
            this.checkActive();
      },
      handlerLink:function(){
      },
      handlerNext:function(){},
      handlerPrev:function(){},
      render: function() {
          return (
            /* jshint ignore:start */
                        <ul className="do-pagination">
                              {this.state.reactElm}
                              <RouteHandler {...this.props} />
                        </ul>            
            /* jshint ignore:end */
          );
        }
});

module.exports =PageComponent;